import Mock from 'mockjs'
import { param2Obj } from '@/utils'

// 虚拟数据
const List = []
const count = 100

const baseContent = '我是测试数据我是测试数据'
const image_uri = 'https://wpimg.wallstcn.com/e4558086-631c-425c-9430-56ffb46e70b3'

for (let i = 0; i < count; i++) {
  List.push(Mock.mock({
    id: '@increment',
    timestamp: +Mock.Random.date('T'),
    name: '@first',
    email: '@email',
    title: '@title(5, 10)',
    content_short: '我是测试数据',
    address: baseContent,
    forecast: '@float(0, 100, 2, 2)',
    importance: '@integer(1, 3)',
    'type|1': ['未审核', '审核通过', '审核不通过'],
    'sex|1': ['女', '男', '保密'],
    birthday: '@datetime',
    comment_disabled: true,
    phone: '@integer(13800138000, 18888888888)',
    image_uri,
    platforms: ['a-platform']
  }))
}

export default {
  list: config => {
    const { keyword, page = 1, size = 20, sorts } = param2Obj(config.url)

    let mockList = List.filter(item => {
      if (keyword && item.name.indexOf(keyword) < 0) return false
      return true
    })

    if (sorts === '-id') {
      mockList = mockList.reverse()
    }

    const pageList = mockList.filter(
      (item, index) => index < size * page && index >= size * (page - 1)
    )

    return {
      total: mockList.length,
      list: pageList,
      pages: count / size
    }
  },
  search: config => {
    const { page = 1, size = 20, sorts } = param2Obj(config.body)

    let mockList = List.filter(item => {
      return true
    })

    if (sorts && sorts.indexOf('-id') > 0) {
      mockList = mockList.reverse()
    }

    const pageList = mockList.filter(
      (item, index) => index < size * page && index >= size * (page - 1)
    )

    return {
      total: mockList.length,
      list: pageList,
      pages: count / size
    }
  },
  create: () => ('操作成功'),
  update: () => ('操作成功'),
  remove: () => ('操作成功')
}
